package com.example.studentregistration.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.studentregistration.pojo.DormBuilding;
import com.example.studentregistration.service.DormBuildingService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DormBuildingController {

    @Autowired
    private DormBuildingService dormBuildingService;

    // 新增楼宇
    @PostMapping("/dormBuilding/add")
    public Result<?> add(@RequestBody DormBuilding building) {
        return dormBuildingService.insert(building);
    }

    // 修改楼宇
    @PutMapping("/dormBuilding/update")
    public Result<?> update(@RequestBody DormBuilding building) {
        return dormBuildingService.update(building);
    }

    // 删除楼宇
    @DeleteMapping("/dormBuilding/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        return dormBuildingService.delete(id);
    }

    // 批量删除
    @PostMapping("/dormBuilding/batchDelete")
    public Result<?> batchDelete(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("请选择要删除的记录");
        }
        int result = dormBuildingService.batchDelete(ids);
        if (result > 0) {
            return Result.success("删除成功" + result + "条记录");
        }
        return Result.fail("删除失败");
    }

    // 分页查询
    @GetMapping("/dormBuilding")
    public Result<PageInfo<Object>> queryAll(@RequestParam(defaultValue = "1") Integer currentPage,
                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page page = dormBuildingService.queryAll(currentPage, pageSize);
        System.out.println(page);
        return Result.success(new PageInfo<>(page));
    }

    // 根据ID查询
    @GetMapping("/dormBuilding/queryById/{id}")
    public Result<DormBuilding> queryById(@PathVariable Integer id) {
        return dormBuildingService.queryById(id);
    }

    // 根据楼栋号查询
    @GetMapping("/dormBuilding/queryByNo")
    public Result<DormBuilding> queryByNo(@RequestParam String buildingNo) {
        return dormBuildingService.queryByBuildingNo(buildingNo);
    }

    // 导出楼宇信息
    @GetMapping("/dormBuilding/export")
    public void export(HttpServletResponse response) throws IOException {
        dormBuildingService.exportExcel(response);
    }

    // 导入楼宇信息
    @PostMapping("/dormBuilding/import")
    public Result<?> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(is);
        List<DormBuilding> list = reader.readAll(DormBuilding.class);
        try {
            for (DormBuilding b : list) {
                dormBuildingService.insert(b);
            }
        } catch (Exception e) {
            return Result.fail("导入失败");
        }
        return Result.success("导入成功");
    }
}